package list;

import java.util.HashSet;
import java.util.Set;

public class LeetCode0201 {
    public ListNode removeDuplicateNodes(ListNode head) {
        if (head == null) return null;
        ListNode dummy = new ListNode(-1, head);
        Set<Integer> set = new HashSet<>();
        ListNode p = head;
        ListNode p1 = dummy;
        while(p != null){
            if(!set.add(p.val)){
                p1.next = p.next;
                p = p.next;
                continue;
            }
            p = p.next;
            p1 = p1.next;
        }
        return dummy.next;
    }
}
